package br.com.CF.dao;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class CustomSQLiteOpenHelper extends SQLiteOpenHelper {

	public static final String TABLE_CONTA = "conta";
	public static final String COLUMN_CD_CONTA = "cd_conta";
	public static final String COLUMN_NM_CONTA = "nm_conta";
	public static final String COLUMN_DESC_CONTA = "desc_conta";
	public static final String COLUMN_SALDO_CONTA = "saldo";

	public static final String TABLE_HISTORICO = "historico";
	public static final String COLUMN_CD_HISTORICO = "cd_historico";
	public static final String COLUMN_DESC_HISTORICO = "desc_historico";
	public static final String COLUMN_CD_CONTA_HISTORICO_FK = "cd_conta";
	public static final String COLUMN_VALOR_HISTORICO = "valor";
	public static final String COLUMN_DATA_HISTORICO = "data_historico";

	private static final String DATABASE_NAME = "notes.db";
	private static final int DATABASE_VERSION = 1;

	// Database creation sql statement
	private static final String CREATE_TABLE_CONTA = "CREATE TABLE IF NOT EXISTS " + TABLE_CONTA + "(" + 
			COLUMN_CD_CONTA + " integer primary key autoincrement, " + 
			COLUMN_NM_CONTA + " text not null, " + 
			COLUMN_DESC_CONTA + " text not null, " + 
			COLUMN_SALDO_CONTA + " real not null);";

	private static final String CREATE_TABLE_HISTORICO = "CREATE TABLE IF NOT EXISTS "+ TABLE_HISTORICO + "("+ 
			COLUMN_CD_HISTORICO + " integer primary key autoincrement, " + 
			COLUMN_DESC_HISTORICO + " text not null, " + 
			COLUMN_CD_CONTA_HISTORICO_FK + " integer not null, " + 
			COLUMN_VALOR_HISTORICO + " real not null, " + 
			COLUMN_DATA_HISTORICO + " text not null);";
	
	public CustomSQLiteOpenHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase database){
		Log.i("ControleFinanceiro", CREATE_TABLE_CONTA);
		database.execSQL(CREATE_TABLE_CONTA+CREATE_TABLE_HISTORICO);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTA);
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_HISTORICO);
		onCreate(db);
	}
}